﻿using System;
using System.Web.UI.WebControls;
using ART.Common;
using ART.Model;
using ART.BLL;

namespace ART.Admini.Setting
{
    public partial class DictionaryDetail : System.Web.UI.Page
    {

        private M_dictionary model = new M_dictionary();
        private B_dictionary bll = new B_dictionary();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }
        }
        protected void btnSave_Click(object sender, EventArgs p1)
        {
            if (DictionaryId>0)
            {
                model = bll.GetModel(this.DictionaryId);
                model.Id = DictionaryId;
                model.DictionaryName = this.txtDictionaryName.Text.Trim();
                model.ParentId = int.Parse(this.dropParentId.SelectedValue);
                model.Sort = int.Parse(this.txtSort.Text.Trim());
                model.IfDelete = ccbIDel.Checked?1:0;
                if (this.bll.Update(model))
                {
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg();");
                }
            } 
            else
            {
                model.DictionaryName = this.txtDictionaryName.Text.Trim();
                model.ParentId = int.Parse(this.dropParentId.SelectedValue);
                model.Sort = int.Parse(this.txtSort.Text.Trim());
                model.MainDictionaryId = this.MainDictionaryId;
                model.IfDelete = ccbIDel.Checked ? 1 : 0; ;
                model.Id = bll.GetMaxId();
                if (bll.Add(model))
                {
                    string CacheKey = "BLL/dictionary/datatable/_" + model.ParentId;
                    ART.Cache.LRSCache.GetCacheService().RemoveObject(CacheKey);

                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg();");
                }
            }
            
        }
        private void InitData()
        {
            JBind.BindList(dropParentId, JTree.GetClassTree(bll.GetListByParent(MainDictionaryId), "ParentId", "DictionaryName"));
            dropParentId.Items.Insert(0, new ListItem("一级分类", "0"));

            if (DictionaryId > 0)
            {
                //if (this.DictionaryId == 0x8d)
                //{
                //    this.txtDictionaryName.Enabled = false;
                //}
                model = bll.GetModel(this.DictionaryId);
                this.txtDictionaryName.Text = model.DictionaryName;
                this.dropParentId.SelectedValue = model.ParentId.ToString();
                this.txtSort.Text = model.Sort.ToString();
                ccbIDel.Checked = model.IfDelete==1?true:false;
            }
            else
            {
                this.dropParentId.SelectedValue = MainDictionaryId.ToString();
            }
        }
        protected int MainDictionaryId
        {
            get
            {
                int num = 0;
                int.TryParse(base.Request.QueryString["MainDictionaryId"], out num);
                return num;
            }
        }
        protected int DictionaryId
        {
            get
            {
                int result = 0;
                int.TryParse(base.Request.QueryString["DictionaryId"], out result);
                return result;
            }
        }
    }
}
